package com.ems.service.impl.Invigilate.room;

import com.ems.dao.Invigilate.room.RoomPaperDao;
import com.ems.pojo.Invigilate.room.RoomPaper;
import com.ems.pojo.Invigilate.room.TestScoreResult;
import com.ems.service.Invigilate.room.ExaminationService;
import com.ems.service.Invigilate.room.RoomPaperService;
import com.ems.service.Invigilate.room.TestScoreResultService;
import com.ems.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 伟伟
 * @Date: 2020/2/18
 * @Description: 描述
 * @Version: 1.0
 */
@Service("roomPaperService")
public class RoomPaperServiceImpl implements RoomPaperService {
    @Autowired
    private RoomPaperDao roomPaperDao;

    @Autowired
    @Qualifier("testScoreResultService")
    private TestScoreResultService testScoreResultService;

    @Autowired
    @Qualifier("examinationService")
    private ExaminationService examinationService;

    //获取试题数量
    @Override
    public List<RoomPaper> getRoomPaperByTrId(int tr_id) {
        return roomPaperDao.getRoomPaperByTrId(tr_id);
    }

    //根据条件查询指定考场中的试题信息
    @Override
    public List<RoomPaper> selectRoomPaperByTrId(Map<String, Object> map) {
        map.put("time",Tools.getTime()); //获取当前的时间
        List<RoomPaper> list = roomPaperDao.selectRoomPaperByTrId(map);
        RoomPaper roomPaper = null;
        long beginMillisecond = 0;
        long endMillisecond = 0;
        long nowMillisecond = 0;
        for(int i = 0;i < list.size();i ++){
            roomPaper = list.get(i);
            beginMillisecond = Tools.getMillisecon(roomPaper.getEp_start_time());
            endMillisecond = Tools.getMillisecon(roomPaper.getEp_end_time());
            nowMillisecond = new Date().getTime();
            if(nowMillisecond >= beginMillisecond && nowMillisecond <= endMillisecond){
                roomPaper.setRp_state(1); //考试进行中
            }else if(nowMillisecond > endMillisecond){
                roomPaper.setRp_state(2); //考试已结束
            }
        }
        //查询试题信息
        return list;
    }

    @Override
    public List<RoomPaper> selectRoomPaperByTrIdAndUser(Map<String, Object> map,int id) {
        List<RoomPaper> list = selectRoomPaperByTrId(map);

        if(list != null && list.size() > 0){
            RoomPaper roomPaper = null;
            for(int i = 0;i < list.size();i ++){
                roomPaper = list.get(i);
                //根据用户id和rp_id在test_score_result表中查询试题的考试考试状态（已完成，未完成）
                TestScoreResult testScoreResult = testScoreResultService.examinationStatusQuery(id,roomPaper.getRp_id());
                if(testScoreResult != null && testScoreResult.getTsr_id() != 0){
                    roomPaper.setTest_status(1);  //已完成考试
                }
            }
        }
        return list;
    }

    //修改考试信息
    @Override
    public void updateRoomPaper(RoomPaper roomPaper) {
        roomPaperDao.updateRoomPaper(roomPaper);
    }

    //删除考试信息
    @Override
    public void deleteRoomPaper(RoomPaper roomPaper) {
        //删除考生在该考场中指定考卷上保存的考试信息
        testScoreResultService.deleteTestScoreResultByRpId(roomPaper.getRp_id());
        //把考卷从考场中删除
        roomPaperDao.deleteRoomPaper(roomPaper);
    }

    //在指定考场中添加考题
    @Override
    public void addRoomPaper(RoomPaper roomPaper) {
        roomPaper.setRp_time(Tools.getTime());
        roomPaperDao.addRoomPaper(roomPaper);
    }


    @Override
    public List<RoomPaper> getRoomPaperByEpId(int ep_id) {
        return roomPaperDao.getRoomPaperByEpId(ep_id);
    }

    @Override
    public void deleteRoomPaperByTrId(int tr_id) {
        List<RoomPaper> roomPaperList = roomPaperDao.queryRoomPaperByTrId(tr_id);

        if(roomPaperList != null && roomPaperList.size() > 0){
            RoomPaper roomPaper = null;
            for(int i = 0;i < roomPaperList.size();i ++){
                roomPaper = roomPaperList.get(i);

                //删除test_score_result表中的信息
                testScoreResultService.deleteTestScoreResultByRpId(roomPaper.getRp_id());

                //删除room_paper信息
                roomPaperDao.deleteRoomPaper(roomPaper);

                //删除examination_paper信息
                examinationService.deleteExaminationByEpId(roomPaper.getEp_id());

            }
        }

    }
}
